package com.example.jdk8.stream_test;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.IntStream;
import java.util.stream.Stream;

public class StreamDeep01 {
    public static void main(String[] args) {
        /**
         * 数据流如何工作
         */
        List<String> list = initList();
        // list 搞成流找出以c 开头的元素，把元素转换成大写，排序后进行输出
        list.stream().filter(s -> s.startsWith("c")).map(String::toUpperCase).sorted().forEach(System.out::println);

        /**
         * 串行流
         */
        System.out.println("============串行流第一种方式=============");
        Arrays.asList("a1", "a2", "a3")
                .stream()
                .findFirst()
                .ifPresent(System.out::println);
        System.out.println("============串行流第二种方式=============");
        Stream.of("b1", "b2", "b3", 4, 5.0)
                .findFirst()
                .ifPresent(System.out::println);
        System.out.println("============IntStream 流=============");
        IntStream.of(1, 3, 56, 2, 36, 25)
                .sorted()
                .forEach(System.out::println);
        IntStream.range(1, 10).forEach(System.out::println);
        System.out.println("============map 流=============");
        Map<String, Object> map = initMap();
        Stream.of(map).forEach(System.out::println); // {1=a1, 2=a2, 3=a3, 4=a4, 5=a5}
        System.out.println("============操作基本数据流=============");
        Arrays.stream(new int[]{1, 2, 3})
                .map((s) -> s * 2 + 1)
                .average()
                .ifPresent(System.out::println); // 5.0
        System.out.println("============对象数据流转换为基本数据流=============");
        Arrays.stream(new String[]{"a1", "a2", "a3"})
                .map(s -> s.substring(1))
                .mapToInt(Integer::valueOf)
                .max()
                .ifPresent(System.out::println); // 3
        System.out.println("============基本数据流转换成对象数据流=============");
        IntStream.range(1, 3)
                .mapToObj(n -> "c" + n)
                .forEach(System.out::println); // c1  c2
        System.out.println("============组合示例=============");
        Stream.of(1.0, 2.0, 3.0)
                .mapToInt(Double::intValue)
                .mapToObj(i -> "a" + i)
                .forEach(System.out::println);
        System.out.println("============流处理顺序=============");
        /**
         * 处理顺序
         * 1. 先排序
         * 2. 过滤
         * 3. 再循环处理
         * filter: a0
         * foreach: a0
         * filter: a1
         * foreach: a1
         * filter: a2
         * foreach: a2
         * filter: a3
         * foreach: a3
         * filter: c1
         * foreach: c1
         */
        Arrays.stream(new String[]{"a1", "a0", "c1", "a2", "a3"})
                .sorted()
                .filter(n -> {
                    System.out.println("filter: " + n);
                    return true;
                })
                .forEach(s -> System.out.println("foreach: " + s));
        System.out.println("============流处理顺序元素大写=============");
        /**
         * map: d2
         * anyMatch: D2
         * map: a2
         * anyMatch: A2
         */
        Stream.of("d2", "a2", "b1", "b3", "c")
                .map(s -> {
                    System.out.println("map: " + s);
                    return s.toUpperCase();
                })
                .anyMatch(s -> {
                    System.out.println("anyMatch: " + s);
                    return s.startsWith("A");
                });
    }

    /**
     * 初始化 map
     *
     * @return map
     */
    private static Map<String, Object> initMap() {
        Map<String, Object> map = new HashMap<>();
        map.put("1", "a1");
        map.put("2", "a2");
        map.put("3", "a3");
        map.put("4", "a4");
        map.put("5", "a5");
        return map;
    }


    /**
     * 初始化数据
     *
     * @return list
     */
    private static List<String> initList() {
        return Arrays.asList("a1", "a2", "b1", "c2", "c1");
    }
}
